package com.care.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.care.entity.Admin;
import com.care.entity.Doctor;
import com.care.entity.User;
import com.care.service.DoctorServiceImpl;
import com.care.service.KnowledgeServiceImpl;
import com.care.ui.DocotorPage;
import com.care.ui.KnowledgePage;

import sun.util.calendar.JulianCalendar;

/**
 * 
 * @author jiangxiongjie
 *	2020年3月9日 下午5:21:41
 */
@Controller
public class DoctorController {
	@Autowired
	private DoctorServiceImpl  serviceImpl;
	@Autowired
	private KnowledgeServiceImpl KnowledgeServiceImpl;
	
	
	
	@RequestMapping("registerdoctor")
	public String DoctorRegister(@RequestParam("img") MultipartFile[] files,HttpServletRequest request,HttpServletResponse response,Doctor doctor) throws IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");		
		String email = request.getParameter("email");	
		
		if (serviceImpl.RegisterUnique(email)) {
			serviceImpl.DoctorRegister(doctor);
			response.getWriter().write("<script> alert('注册成功');window.location='login.jsp'</script>");		
		}
		else {
			response.getWriter().write("<script> alert('当前邮箱已经被注册,请注册其他账号');window.location='registerDoctor.jsp'</script>");
		}
		return null;		
	}
	private ServletContext getServletContext() {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 实现多个用户登录
	 * @param request
	 * @param response
	 * @param doctor
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("login")
	public String Login(HttpServletRequest request,HttpServletResponse response,Doctor doctor,Admin admin,User user)throws IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;chaset=utf-8");
		HttpSession session=request.getSession();		
		String who=request.getParameter("who");
		String email=request.getParameter("email");				
		if (who.equals("医生")&&this.serviceImpl.DoctorLogin(doctor)) {				
			doctor=this.serviceImpl.FindDoctorByEmail(email);			
			session.setAttribute("radio", who);			
			session.setAttribute("doctor",doctor);
			response.getWriter().write("<script> alert('欢迎登录,您辛苦了');window.location='DoctorPage'</script>");								
		}
		else if (who.equals("管理员")&&this.serviceImpl.AdminLogin(admin)){
			admin=this.serviceImpl.FindAdminByEmail(email);
			session.setAttribute("radio", who);
			session.setAttribute("admin", admin);
			response.getWriter().write("<script> alert('登录成功');window.location='index.jsp'</script>");
			
		}
		else if(who.equals("用户")&&this.serviceImpl.UserLogin(user)){
			user=this.serviceImpl.FindUserByEmail(email);
			session.setAttribute("radio", who);
			session.setAttribute("user", user);		
			response.getWriter().write("<script> alert('欢迎您,登录成功');window.location='DoctorPage'</script>");
		} else {	
			response.getWriter().write("<script> alert('当前账号不存在');window.location='login.jsp'</script>");	
		}	
	
		return null;		
	}
	/**
	 * 退出登录
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("Exit")
	public String Exit(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session=request.getSession();
		session.invalidate();
		response.getWriter().write("<script> alert('退出成功');window.location='login.jsp'</script>");
		return null;
		
		
		
	}
	/**
	 * 主页医生分页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("DoctorPage")
	public String DoctorPage(HttpServletRequest request,HttpServletResponse response){				
		HttpSession session=request.getSession();	
		//医生分页	
		String number=request.getParameter("pageNum");
		int pageNum=0;
		if ( number==null || number.equals("")) {
			 pageNum=1;			
		}
		else {
			pageNum=Integer.parseInt(number);
		}	
		List<Doctor> doctorPages=this.serviceImpl.FindDoctorByPage(pageNum,4);
		int countDoctor=this.serviceImpl.CountDoctor();
		DocotorPage<Doctor> docotorPage= new DocotorPage<Doctor>(pageNum, 4);
		docotorPage.setList(doctorPages);
		docotorPage.setTotalCountPage(countDoctor);
		session.setAttribute("docotorPage", docotorPage);
		return "/KnowledgePage";		
	}
	/**
	 * 医生页面全部分页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("AllDocotrPage")
	public String AllDoctorPage(HttpServletRequest request,HttpServletResponse response){
		HttpSession session=request.getSession();
		String number=request.getParameter("pageNum");
		int pageNum=0;
		if ( number==null || number.equals("")) {
			pageNum=1;			
		}
		else {
			pageNum=Integer.parseInt(number);
		}	
		List<Doctor> alldoctorPages=this.serviceImpl.FindDoctorByPage(pageNum, 8);
		int countAllDoctor=this.serviceImpl.CountDoctor();
		DocotorPage<Doctor> alldoctorpage=new DocotorPage<Doctor>(pageNum,8);
		alldoctorpage.setList(alldoctorPages);
		alldoctorpage.setTotalCountPage(countAllDoctor);
		session.setAttribute("alldoctorpage", alldoctorpage);
		return "/doctors.jsp";		
	}
	/**
	 * 科室分页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("SectionPage")
	public String SectionPage(HttpServletRequest request,HttpServletResponse response){
		HttpSession session=request.getSession();
		String number=request.getParameter("pageNum");
		String section=request.getParameter("section");		
		int pageNum=0;
		if ( number==null || number.equals("")) {
			pageNum=1;			
		}
		else {
			pageNum=Integer.parseInt(number);
		}		
		List<Doctor> sectionPage=this.serviceImpl.FindDoctorBySection(pageNum, 4, section);
		int countSectionPage=this.serviceImpl.CountSectionDoctor(section);
		DocotorPage<Doctor> sectionpage=new DocotorPage<Doctor>(pageNum,4);
		sectionpage.setList(sectionPage);
		sectionpage.setTotalCountPage(countSectionPage);
		session.setAttribute("sectionpage", sectionpage);
		if (section.equals("内科")) {
			
			return "/departments-single.jsp";			
		}
		else if (section.equals("外科")) {
			return "/departments-two.jsp";			
		}
		else if (section.equals("精神科")) {
			return "/departments-three.jsp";			
		}
		
		return null;
	}
	/**
	 * 查找医生信息显示到文案
	 * @param request
	 * @param response
	 * @param doctor
	 * @return
	 */
	@RequestMapping("findDoctor")
	public String findDoctor(HttpServletRequest request,HttpServletResponse response,Doctor doctor){
		HttpSession session=request.getSession();
		 Doctor email=(Doctor) session.getAttribute("doctor");		
		Doctor doctor1=this.serviceImpl.findDoctor(email.getEmail());		
		session.setAttribute("doctor1", doctor1);
		return "/appointment.jsp";
	}
	/**
	 * 更新医生信息
	 * @param request
	 * @param response
	 * @param doctor
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("updateDoctor")
	public String updateDoctor(HttpServletRequest request,HttpServletResponse response,Doctor doctor) throws IOException{
		HttpSession session=request.getSession();
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String information=request.getParameter("information");
		String name=request.getParameter("name");
		String section=request.getParameter("section");
		String sex=request.getParameter("sex");
		String password=request.getParameter("password");		
		String img=request.getParameter("img");
		doctor=(Doctor) session.getAttribute("doctor1");
		this.serviceImpl.updateDoctor(doctor, name, img, information, section, sex, password);	
		response.getWriter().write("<script> alert('修改成功');window.location='findDoctor'</script>");
		return null;		
	}
	/**
	 * 查询信息返回到文案上
	 * @param request
	 * @param response
	 * @param doc
	 * @return
	 */
	@RequestMapping("checkMoney")
	public String checkMoney(HttpServletRequest request,HttpServletResponse response,Doctor doc){
		HttpSession session=request.getSession();
		doc=(Doctor) session.getAttribute("doctor1");
		Doctor money=this.serviceImpl.findDoctor(doc.getEmail());
		request.setAttribute("money", money);
		return "/money.jsp";		
	}
	/**
	 * 用户充值
	 * @param request
	 * @param response
	 * @param doctor
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("updateMoney")
	public String updateMoney(HttpServletRequest request,HttpServletResponse response,Doctor doctor) throws IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session=request.getSession();
		doctor=(Doctor) session.getAttribute("doctor1");
		double money=Double.parseDouble(request.getParameter("money"));
		this.serviceImpl.updateMoney(doctor.getEmail(), money);
		response.getWriter().write("<script> alert('充值成功');window.location='findDoctor'</script>");
		return null;		
	}
	
}
